# 项目目录
import os
from flask import Flask
import pymysql
from home.config import BASE_DIR
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()
def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        # load the instance config, if it exists, when not testing
        CONFIG_PATH = BASE_DIR / "home/config.py"
        app.config.from_pyfile(CONFIG_PATH, silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    pymysql.install_as_MySQLdb()
    db.init_app(app)
    Migrate(app, db)
    # @app.route('/')
    # def hello_world():  # put application's code here
    #     return 'Hello World!'

    # 注册蓝图
    from blue.user import views as user
    from blue.auth import views as auth
    from blue.enterprise import views as enterprise
    from blue.admin import views as admin
    app.register_blueprint(user.bp)
    app.register_blueprint(auth.bp)
    app.register_blueprint(enterprise.bp)
    app.register_blueprint(admin.bp)
    return app
